﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SecurityApi.DataAccess
{
    public class UserClaimRepository : BaseRepository
    {
        public UserClaimRepository()
            : base()
        {
        }

        public UserClaimRepository(SecurityApiEntities context)
            :base(context)
        {
        }

        public IQueryable<UserClaim> GetUserClaims(int userId)
        {
            var query = from u in _context.UserClaims where u.UserId == userId select u;
            return query;
        }

        public int AddUserClaim(int userId, int claimTypeId,int resourceId)
        {
            int rowId = -1;

            try
            {
                UserClaim role = new UserClaim();
                role.UserId = userId;
                role.ResourceId = resourceId;
                role.ClaimTypeId = claimTypeId;
                role.adt_DateCreated = DateTime.Now;
                _context.UserClaims.Add(role);
                _context.SaveChanges();
                rowId = role.UserClaimId;
            }
            catch (Exception ex)
            {
                rowId = -1;
            }

            return rowId;
        }

        public bool RemoveUserClaim(int id)
        {
            bool bResult = true;

            try
            {
                UserClaim userClaim = _context.UserClaims.Where(p => p.UserClaimId == id).SingleOrDefault();
                if (null != userClaim)
                {
                    _context.UserClaims.Remove(userClaim);
                    _context.SaveChanges();
                }
                else
                {
                    bResult = false;
                }
            }
            catch (Exception ex)
            {
                bResult = false;
            }

            return bResult;
        }


    }
}
